﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESOL.Common.Entity;
using Suzsoft.Smart.Data;

namespace ESOL.Business
{
    public class FinCommonFunctions
    {
        public static FinSupplierAccountEntity GetBySupplierId(string supplierId)
        {
            if (string.IsNullOrEmpty(supplierId))
            {
                return null;
            }
            FinSupplierAccountEntity entity = new FinSupplierAccountEntity();
            entity.SupplierId = supplierId;
            FinSupplierAccountEntity account = DataAccess.SelectSingle<FinSupplierAccountEntity>(entity);
            if (account == null)
            {
                account = new FinSupplierAccountEntity();
                account.SupplierAccountId = Guid.NewGuid().ToString();
                account.SupplierId = supplierId;
                account.PurchaseOrderAmount = 0;
                account.BillingAmount = 0;
                account.PaidAmount = 0;
                account.PayableAmount = 0;
                account.ArrivedAmountNoBilling = 0;
                account.ArrivedAmountBilled = 0;
                DataAccess.Insert(account);
            }
            return account;
        }

        public static FinCustomerAccountEntity GetByCustomerId(string customerId)
        {
            if (string.IsNullOrEmpty(customerId))
            {
                return null;
            }
            FinCustomerAccountEntity entity = new FinCustomerAccountEntity();
            entity.CustomerId = customerId;
            FinCustomerAccountEntity account = DataAccess.SelectSingle<FinCustomerAccountEntity>(entity);
            if (account == null)
            {
                account = new FinCustomerAccountEntity();
                account.CustomerAccountId = Guid.NewGuid().ToString();
                account.CustomerId = customerId;
                account.SalesOrderAmount = 0;
                account.BillingAmount = 0;
                account.ReceivedAmount = 0;
                account.ReceivableAmount = 0;
                account.DeliveryAmountNoBilling = 0;
                account.DeliveryAmountBilled = 0;
                account.SalesAmount = 0;
                DataAccess.Insert(account);
            }
            return account;
        }

        public static FinCashAmountEntity GetByCurrency(string currency)
        {
            if (string.IsNullOrEmpty(currency))
            {
                return null;
            }
            FinCashAmountEntity entity = new FinCashAmountEntity();
            entity.Currency = currency;
            return DataAccess.SelectSingle<FinCashAmountEntity>(entity);
        }

        public static void InsertCashFlow(FinCashFlowEntity cashFlow, DataAccessBroker broker)
        {
            DataAccess.Insert(cashFlow, broker);
            FinCashAmountEntity cashAmount = GetByCurrency(cashFlow.Currency);
            if (cashAmount == null)
            {
                cashAmount = new FinCashAmountEntity();
                cashAmount.CashAmountId = Guid.NewGuid().ToString();
                cashAmount.TotalAmount = cashFlow.Amount;
                cashAmount.Currency = cashFlow.Currency;
                DataAccess.Insert(cashAmount, broker);
            }
            else
            {
                cashAmount.TotalAmount += cashFlow.Amount;
                DataAccess.Update(cashAmount, broker);
            }
        }
    }
}
